<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('阿里云tts参数配置')" />
    <th:block th:include="include :: layout-latest-css" />
    <th:block th:include="include :: ztree-css" />
    <style>
    </style>
</head>

<body>
<div class="main-content">
    <div class="h4 form-header" th:text="#{switchconf.tts.ali.header}"></div>
    <div id="baseConfigs"></div>

    <!-- 保存按钮 -->
    <div class="row" ></div>
    <div class="row" >
        <div class="col-sm-offset-5 col-sm-10">
            <button id="saveConfig" type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i><span th:text="#{switchconf.btn.save}"></span></button>&nbsp;
        </div>
    </div>
</div>



<th:block th:include="include :: footer" />
<th:block th:include="include :: layout-latest-js" />
<script>
    // 页面加载完成后，发送AJAX请求获取配置数据
    var prefix = ctx + "cc/fsconf";
    $(function() {
        $.ajax({
            url: prefix + '/getAliTtsConf',
            type: 'GET',
            success: function(_data) {
                // 假设后端返回的数据格式为 [{"aliasName":"xxx", "name":"xxx", "value":"xxx"},{}]
                var baseConfigsHtml = '';
                var data = _data.data;

                // 渲染基础配置
                $.each(data, function(index, config) {
                    baseConfigsHtml += '<div className="row">';
                    baseConfigsHtml += '<div class="col-sm-12">';
                    baseConfigsHtml += '<label class="col-sm-2 control-label">' + config.aliasName + '</label><div class="col-sm-10"><input class="config-value form-control" type="text" id="' + config.name + '" value="' + config.value + '" /></div>';
                    baseConfigsHtml += '</div>';
                    baseConfigsHtml += '</div>';
                });
                $('#baseConfigs').html(baseConfigsHtml);
            },
            error: function(error) {
                console.error('Error fetching configuration:', error);
            }
        });
    });

    // 保存配置
    $('#saveConfig').click(function() {
        var configs = [];
        $('input.config-value').each(function() {
            var config = {
                name: $(this).attr('id'),
                value: $(this).val().trim()
            };
            configs.push(config);
        });
        $.ajax({
            url: prefix + '/setAliTtsConf', // 假设后端保存配置的接口是 /setConfig
            type: 'POST',
            contentType: 'application/json',
            data: JSON.stringify(configs),
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍候...");
                $.modal.disable();
            },
            success: function(response) {
                processAjaxReponseJson(response);
            },
            error: function(error) {
            }
        });
    });

</script>

</body>
</html>